package tool

import (
	"myGame/src/model"
)

//CheckCollision检查人物和障碍物之间是否有碰撞

func CheckCollision(obstacle *model.Obstacle, i int, realm *model.Realm) bool {
	//障碍物的四个角的坐标
	obstacleLeftUpX, obstacleLeftUpY := obstacle.X[i], obstacle.Y[i]
	obstacleLeftDownX, obstacleLeftDownY := obstacle.X[i], obstacle.Y[i]+obstacle.Height[i]
	obstacleRightUPX, obstacleRightUPY := obstacle.X[i]+obstacle.Width[i], obstacle.Y[i]
	obstacleRightDownX, obstacleRightDownY := obstacle.X[i]+obstacle.Width[i], obstacle.Y[i]+obstacle.Height[i]
	//障碍物左上角
	if CheckColl(obstacleLeftUpX, obstacleLeftUpY, realm) {
		return true
	}
	//障碍物左下角
	if CheckColl(obstacleLeftDownX, obstacleLeftDownY, realm) {
		return true
	}
	//障碍物右上角
	if CheckColl(obstacleRightUPX, obstacleRightUPY, realm) {
		return true
	}
	//障碍物右下角
	if CheckColl(obstacleRightDownX, obstacleRightDownY, realm) {
		//fmt.Println(obstacleRightDownX, obstacleRightDownY)
		return true
	}

	return false
}

// 因为那个方法中，4*4有16种碰撞检测，所以我的重新定义方法包装一下检测
// 判断障碍物的四个角是否都会出现在人的内部，如果人是比障碍物小的（下蹲）那就有bug了
// 解决这个bug我决定，让人下蹲的时候，只要比障碍物高就行了
func CheckColl(X, Y float64, realm *model.Realm) bool {
	//realm的四个角的坐标
	LeftX := realm.X + 40
	RightX := realm.X + float64(realm.Width[realm.State]) - 40
	UPY := realm.Y[realm.State]
	DownY := realm.Y[realm.State] + float64(realm.Height[realm.State]) - 40

	//判断这个点是否在Realm的长方体之内
	if X >= LeftX && X <= RightX {
		if Y <= DownY && Y >= UPY {
			return true
		}
	}
	return false
}
